package com.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class Init {
    private static ChromeDriver driver;
    public static ChromeDriver createDriver() {
        if(driver==null){
            WebDriverManager.chromedriver().setup();
            //增加浏览器配置，创建驱动对象，强制允许方法所有连接.
            ChromeOptions chromeOptions=new ChromeOptions();
            chromeOptions.addArguments("--remote-allow-origins=*");
            driver = new ChromeDriver(chromeOptions);
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        }
        return driver;
    }
    public Init(String url) {
        ChromeDriver driver = createDriver();
        driver.get(url);
    }
    public List<String> getDateTime(){
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("yyyy-MM-dd");
        String filename = simpleDateFormat1.format(System.currentTimeMillis());
        String dirname = simpleDateFormat2.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }
    public void getScreenShot(String str) throws IOException {
        List<String> list = getDateTime();
        String filename ="./src/test/java/com/blogWebAutoTest/"+list.get(0)+"/"+str+"_"+list.get(1)+".png";
        File screenshotAs = driver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotAs,new File(filename));
    }
    public void openURL(String url){

    }

}
